import { createRouter, createWebHistory } from "vue-router";

const routes = [
    {
        path: "/",
        name: "Layout",
        component: () => import("../views/Layout.vue"),
        children: [
            {
                path: "",
                name: "Home",
                component: () => import("../views/Home.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "paper",
                name: "PaperList",
                component: () => import("../views/Paper/PaperList.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "paper/add",
                name: "PaperAdd",
                component: () => import("../views/Paper/AddPaper.vue"),
                meta: { requiresAuth: false },
            },
            {
                path: "paper/:id",
                name: "PaperDetail",
                component: () => import("../views/Paper/PaperDetail.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "paper/:id/edit",
                name: "PaperEdit",
                component: () => import("../views/Paper/EditPaper.vue"),
                meta: { requiresAuth: false },
            },
            {
                path: "exam",
                name: "ExamList",
                component: () => import("../views/Exam/ExamList.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "exam/add",
                name: "ExamAdd",
                component: () => import("../views/Exam/AddExam.vue"),
                meta: { requiresAuth: false },
            },
            {
                path: "exam/:id",
                name: "ExamDetail",
                component: () => import("../views/Exam/ExamResults.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "exam/:id/edit",
                name: "ExamEdit",
                component: () => import("../views/Exam/EditExam.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "exam/:id/results",
                name: "ExamResults",
                component: () => import("../views/Exam/ExamResults.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "course",
                name: "CourseList",
                component: () => import("../views/Course/CourseList.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "course/add",
                name: "CourseAdd",
                component: () => import("../views/Course/AddCourse.vue"),
                meta: { requiresAuth: false },
            },
            {
                path: "course/:id",
                name: "Chapter",
                component: () => import("../views/Course/Chapter.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "course/:id/edit",
                name: "CourseEdit",
                component: () => import("../views/Course/EditCourse.vue"),
                meta: { requiresAuth: false },
            },
            {
                path: "question",
                name: "QuestionList",
                component: () => import("../views/Question/QuestionList.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "question/add",
                name: "QuestionAdd",
                component: () => import("../views/Question/AddQuestion.vue"),
                meta: { requiresAuth: false },
            },
            {
                path: "question/:id/edit",
                name: "QuestionEdit",
                component: () => import("../views/Question/EditQuestion.vue"),
                meta: { requiresAuth: false },
            },
            {
                path: "user",
                name: "UserList",
                component: () => import("../views/User/UserList.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: '/userInfo',
                name: 'UserInfo',
                component: () => import('../views/User/UserInfo.vue'),
                meta: { requiresAuth: false, title: '个人信息' }
            },
        ],
    },
    {
        path: "/login",
        name: "Login",
        component: () => import("../views/Login.vue"),
        meta: { requiresAuth: false },
    },
    {
        path: "/do-exam/:id",
        name: "DoExam",
        component: () => import("../views/Exam/DoExam.vue"),
        meta: { requiresAuth: true },
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem("token");
    if (to.meta.requiresAuth && !token) {
        next("/Login");
    } else {
        next();
    }
});

export default router;
